package com.luping.xiao.xiao.utils;

import net.sf.json.JSONObject;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

public class Test {

    private static final Log log = LogFactory.getLog(Test.class);
    private static ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(200,
            new BasicThreadFactory.Builder().namingPattern("url-schedule-pool-%d").daemon(true).build());

    public static int num =1;


//    public static void main(String[] str) {
//        for (int i = 0; i < 1000; i++) {
//            HttpRun();
//        }
//    }


    public static void main(String[] str){
        List<DeviceUpgrade> list=new ArrayList<>();
        for(int i=0;i<8;i++){
            DeviceUpgrade u=new DeviceUpgrade();
            u.setCarNum("7003"+i);
            u.setLineId(189);
            u.setLineName("134");
            u.setFileName("974.sw");
            list.add(u);
        }
        for(int i=0;i<8;i++){
            DeviceUpgrade u=new DeviceUpgrade();
            u.setCarNum("6803"+i);
            u.setLineId(201);
            u.setLineName("20");
            u.setFileName("974.sw");
            list.add(u);
        }
        for(int i=0;i<8;i++){
            DeviceUpgrade u=new DeviceUpgrade();
            u.setCarNum("7003"+i);
            u.setLineId(189);
            u.setLineName("134");
            u.setFileName("9757.sw");
            list.add(u);
        }
         net.sf.json.JSONObject lineJson=new net.sf.json.JSONObject();
        for(DeviceUpgrade l:list){
            net.sf.json.JSONArray carNumList=new net.sf.json.JSONArray();
            net.sf.json.JSONObject carNum=new net.sf.json.JSONObject();
            net.sf.json.JSONArray carFileList=new net.sf.json.JSONArray();
            net.sf.json.JSONObject carFile=new net.sf.json.JSONObject();
            carFile.put("fileName",l.getFileName());
            carFileList.add(carFile);
           boolean lineIdCon=lineJson.containsKey(l.getLineId()+"");
           boolean carNumCon=false;
           if(lineIdCon){
               net.sf.json.JSONArray carnumsList=lineJson.getJSONArray(l.getLineId()+"");
               for(int i=0;i<carnumsList.size();i++){
                   net.sf.json.JSONObject carnumsDetail=carnumsList.getJSONObject(i);
                   if(carnumsDetail.containsKey(l.getCarNum())){
                       carNumCon=true;
                       break;
                   }
               }
               if(carNumCon){
                   net.sf.json.JSONArray carFL=lineJson.getJSONArray(l.getLineId()+"");
                   for(int i=0;i<carFL.size();i++){
                       net.sf.json.JSONObject carNumDetail= JSONObject.fromObject(carFL.get(i).toString());
                       if(carNumDetail.containsKey(l.getCarNum())){
                           net.sf.json.JSONArray carNumDetailList=new net.sf.json.JSONArray();
                           carNumDetailList.addAll(carNumDetail.getJSONArray(l.getCarNum()));
                           carNumDetailList.addAll(carFileList);
                           carNum.put(l.getCarNum(),carNumDetailList);
                           carFL.remove(i);
                           break;
                       }
                   }
               }else{
                   carNum.put(l.getCarNum(),carFileList);
               }
               carNumList=lineJson.getJSONArray(l.getLineId()+"");
               carNumList.add(carNum);
               lineJson.put(l.getLineId()+"",carNumList);
           }
           else{
               carNum.put(l.getCarNum(),carFileList);
                carNumList.add(carNum);
                lineJson.put(l.getLineId()+"",carNumList);
            }

        }

        log.info((lineJson));
       // log.info(lineJson.toJSONString());

    }



    public static void HttpRun() {
        executorService.execute(new TestThread());
    }
}
